package com.ivan.stu.notetool.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.ivan.stu.notetool.R;
import com.ivan.stu.notetool.utils.ImageUtils;


import java.util.List;

/**
 * Created by IVAN on 2019/3/12.
 */

public class MyPagerAdapter extends PagerAdapter {
    private List<View> list;
    private Context mContext;
    private List<String> imageList;

    private GestureDetector gestureDetector;
    private  onSingleClickListener listener;

    public MyPagerAdapter(List<String> imageList, Context context) {


        this.mContext = context;
        this.imageList = imageList;

        gestureDetector = new GestureDetector(mContext,new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onSingleTapUp(MotionEvent e) {

                if (listener !=null){

                    listener.onSingleClick();
                }

                return true;
            }
        });

    }

    public interface onSingleClickListener{

        void onSingleClick();
    }

    public  void setOnSingleClickLisner(onSingleClickListener listener){

        this.listener = listener;
    }


    @Override
    public int getCount() {

        if (imageList != null && imageList.size() > 0) {
            return imageList.size();
        } else {
            return 0;
        }
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }


    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {


        final View view = LayoutInflater.from(mContext).inflate(
                R.layout.view_pager_item, container,false);   //绑定viewpager的item布局文件
        final SubsamplingScaleImageView iv =  view.findViewById(R.id.view_image);   //绑定布局中的id
        Glide.with(mContext).load(imageList.get(position)).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {

                Bitmap bitmap = ImageUtils.drawableToBitmap(resource);
                iv.setImage(ImageSource.bitmap(bitmap));
            }
        });

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (listener!=null){

                    listener.onSingleClick();
                }

            }
        });


        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (listener!=null){

                    listener.onSingleClick();
                }
            }
        });


        container.addView(view);
        return view;
    }
//
//        @Override
//        public int getItemPosition(Object object) {
//            return POSITION_NONE;
//    }
}
